#coding=utf-8
'''
Created on 2009-5-5

@author: Administrator
'''
import os
import sys
#sys.path.append('modules')
from base import *
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

#数据模型
class BlogData(db.Model):
    author = db.UserProperty()
    title = db.StringProperty()
    body = db.TextProperty()
    date = db.DateTimeProperty(auto_now_add=True)

#跳到首页    
class Index(BlogRequestHandler):
    global siteroot
    siteroot = r'^[\w-\/]*$'
    def get(self):
        blogdatas = db.GqlQuery('SELECT * FROM BlogData ORDER BY date DESC LIMIT 7')
        template_value = {
                          'blogdatas':blogdatas,
                        }
        path = os.path.join(os.path.dirname(__file__), r'templates/sheji.html')
        self.response.out.write(template.render(path,template_value))

#帐号登录，未完善 5/5/2009
class logIn(BlogRequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            nickname = user.nickname()
            blogdatas = db.GqlQuery('SELECT * FROM BlogData ORDER BY date DESC LIMIT 7')
            template_value = {
                          'nickname':nickname,
                          'blogdatas':blogdatas,
                          }
            path = os.path.join(os.path.dirname(__file__), r'templates/admin.html')
            self.response.out.write(template.render(path,template_value))
            
        else:
            self.redirect(users.create_login_url(self.request.uri))

#帐号退出，未完善 5/5/2009
class logOut(BlogRequestHandler):
    def get(self):
        self.redirect(r'/')

#跳到写博客页面
class newPost(BlogRequestHandler):
    def get(self):
        template_value = {
                          }
        path = os.path.join(os.path.dirname(__file__), r'templates/newPost.html')
        self.response.out.write(template.render(path,template_value))
        
class Sign(BlogRequestHandler):
    def post(self):
        blogdata = BlogData()
        if users.get_current_user():
            blogdata.author = users.get_current_user()
        else:
            blogdata.author = '陌生人'
        blogdata.body = self.request.get('body')
        blogdata.title = self.request.get('title')
        blogdata.put()
        self.redirect(r'/')

def url_mapping():
    global siteRoot
    global url_mapping
    url_mapping = [
                   ('/',Index),
                   ('/login',logIn),
                   ('/sign',Sign),
                   ('/index',Index),
                   ('/logout',logOut),
                   ('/newPost',newPost),
                   ]
    
url_mapping()

def main():
    mapping = url_mapping
    application = webapp.WSGIApplication(
            mapping,
            debug=True)
    run_wsgi_app(application)

if __name__ == '__main__':
    main()